﻿using AyaWo.Commen;
using AyaWo.DbF;
using AyaWo.WinForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
using AyaWo.DAL;
using BellesLettres;
using XiuLetters;
using YuLe163Hot;
using Qbqqban;

namespace AyaWo.WinFrom
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
            // var va = "server=122.112.229.171;user id=Sp;password=ChinaZ_55512hbCf3tLiEOp;database=ayawoWeb;charset=utf8";
            try
            {
                ///存储链接
                var va = DatHelper<string>.GetDatStr("connStr.dat");
                try {
                    var ser = ServicesMothe.IsRun(serviceName);
                    if (ser)
                    {
                        enable(false);
                    }
                    else
                    {
                        enable(true);
                    }
                }
                catch
                {
                    enable(false);
                }

                try
                {
                    var aaa = spcontentService.GetSqliteList();
                    dataGridView1.DataSource=   aaa; 
                  
                }
                catch (Exception ex)
                {

                }
                try
                {
                    dataGridView2.DataSource = Logser.GetSqliteList();
                }
                catch
                {
                }

                try {
                    button7.Visible = false;
                    button13.Visible = false;
                    button14.Visible = false;
                    button15.Visible = false;
                    button16.Visible = false;
                    button17.Visible = false;
                    button18.Visible = false;
                    button19.Visible = false;

                    var blsp = new BellesLettresSpler();
                    button7.Text = blsp.SplerName;
                    button7.Visible = true;

                    XiuLettersSpler xlsp = new XiuLettersSpler();
                    button13.Text = xlsp.SplerName;
                    button13.Visible = true;

                    YuLe163HotSpler ylsp = new YuLe163HotSpler();
                    button14.Text = ylsp.SplerName;
                    button14.Visible = true;

                    QbqqbanAuto qba = new QbqqbanAuto();
                    button15.Text = qba.SplerName;
                    button15.Visible = true;
                    
                }
                catch { }
                
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("编辑按钮");
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //AddWebSite aws = new AddWebSite();
            //this.Hide();
            //if (aws.ShowDialog() == DialogResult.OK)
            //{
            //    this.Show();
            //}  
        }
        
        //string serviceFilePath = $"{Application.StartupPath}\\AyaWoWinSer.exe";
        string serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\AyaWoWinSer.exe";
        string serviceName = "AyaWoWinSer";
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                var ser = ServicesMothe.IsServiceExisted(serviceName);
                if (!ser)
                    ServicesMothe.InstallService(serviceFilePath);
                if (ServicesMothe.IsServiceExisted(serviceName))
                {
                    ServicesMothe.ServiceStart(serviceName);
                    enable(false);
                    label2.Text = "服务已启用";
                }
                else
                {
                  
                }
            }
            catch(Exception ex)
            {
                var t = ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (ServicesMothe.IsServiceExisted(serviceName))
                {
                    ServicesMothe.UninstallService(serviceFilePath);
                    MessageBox.Show("卸载成功...");
                }
                else
                {
                    MessageBox.Show("不存在该服务...");
                }
            }
            catch { }
        }
        /// <summary>
        /// 关闭服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServicesMothe.IsServiceExisted(serviceName))
                {
                    ServicesMothe.ServiceStop(serviceName);
                }
                enable(true);
                label2.Text = "服务未启用";
            }
            catch { }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///退出登陆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
           Caches.Remove("userInfo");
            this.Hide();
            LogForm lf = new LogForm();
            if (lf.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ConnForm connForm = new ConnForm();
            connForm.ShowDialog();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            SoForm so = new SoForm();
            so.ShowDialog();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            new HZForm().ShowDialog();
        }

        void enable(bool bol)
        {
            button1.Enabled =  button12.Enabled =button3.Enabled =   button5.Enabled= bol;
            button4.Enabled = !bol;
            if (bol)
            {
                button3.BackColor = Color.DodgerBlue;
              //  button4.BackColor = Color.Crimson;
                button12.BackColor = Color.MediumSeaGreen;
                button1.BackColor = Color.Orange;
               // button2.BackColor = Color.BlueViolet;
                button5.BackColor = Color.Red;

               button4.BackColor = Color.Transparent;
            }
            else
            {
                button3.BackColor =button12.BackColor =button1.BackColor = button5.BackColor = Color.Transparent;
                button4.BackColor = Color.Crimson;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var sp = new BellesLettresSpler();
            TonbuForm tf = new TonbuForm(sp.DatName);
            this.Hide();
            if (tf.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            var sp = new XiuLettersSpler();
            TonbuForm tf = new TonbuForm(sp.DatName);
            this.Hide();
            if (tf.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            var sp = new YuLe163HotSpler();
            TonbuForm tf = new TonbuForm(sp.DatName);
            this.Hide();
            if (tf.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            var qba = new QbqqbanAuto();
            SoForm tf = new SoForm(qba.DatName);
         
            if (tf.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {

        }

        private void button17_Click(object sender, EventArgs e)
        {

        }

        private void button18_Click(object sender, EventArgs e)
        {

        }

        private void button19_Click(object sender, EventArgs e)
        {

        }
    }

    /// <summary>
    /// 服务方法
    /// </summary>
    public class ServicesMothe
    {
        //判断服务是否存在
        public static bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        //安装服务
        public static void InstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }

        }

        //卸载服务
        public static void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }

        }
        //启动服务
        public static void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }

        }

        //停止服务
        public static void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }

        public static bool IsRun(string serviceName)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceController serviceController = new ServiceController(serviceName);
                if (serviceController.Status == ServiceControllerStatus.Running) { return true; }
                else return false;
            }
            else
            {
                return false;
            }
        }
    }
}
